
***************************************************
***************************************************
**********                               ********** 
********** Creating Affluent Generations ********** 
**********                               **********    
***************************************************
***************************************************

clear all
global beg_path `" XXX "' // Indicate your working directory
set more off

* Opening the dataset with elections (to get the country-years)
use `"${beg_path}Data\Macro\Base_dataset"', replace
gen country_to_merge = country 

* Uploading the UN population data
merge m:1 country_to_merge year using `"${beg_path}Data\Macro\UN_gen.dta"' 

list country country_to_merge year if _merge==1 // this looks ok (UN data are available only until 2015)
drop _merge
order country UN_gen_country_temp // looks good 
drop if country ==.  

*Checking the correlation between the UN data and electorate size (from electoral handbooks)
egen temp1 = rowtotal(UN_gen_G_80and_more UN_gen_a_75-UN_gen_a_18)
egen temp2 = rownonmiss(UN_gen_G_80and_more UN_gen_a_*) 
replace temp1 = . if temp2 == 0
pwcorr temp1 electorate_size // correlation between voting age population size (UN) with electorate size (from electoral handbooks, rounded to millions) is 0.996. 
drop temp1  

*Incorporating data on GDP 
merge m:1 country_to_merge year using `"${beg_path}Data\Macro\gdp.dta"'  
// perfect merge 
drop _merge 
drop if country ==.  

*Getting rid of individuals that have not reached the voting age 
foreach var of varlist UN_gen_a_* {
local x = substr("`var'", -2, 2)
replace `var' = . if `x' < Voting_age
}

*Generating the total of voting age population
egen total_VAP = rowtotal(UN_gen_a_* UN_gen_G_80and_more) 
 
*Generating year of birth
foreach var of varlist UN_gen_a* {
local x = substr("`var'", -2, 2)
gen yob_`var' = year - `x'
}
gen yob_UN_gen_G_80and_more = year - 80

*Generating variables with affluent generations (that will be summed)
*Belarus (7), Latvia (59), Russia (92), Trinidad (111) reached the reference GDP ($12,000) and experienced a decline before reaching it again. 
*This is reflected in the following code: 

fre country, all
foreach var of varlist UN_gen_a* {
gen n_`var' = . 
replace n_`var' = `var' if (yob_`var' >= GDP_12000_start_year & GDP_12000_start_year!=.) | (country_to_merge == 7 & yob_`var' > 1976 & yob_`var' < 1980)  | (country_to_merge == 59 & yob_`var' > 1974 & yob_`var' < 1981) |  ///
(country_to_merge == 92 & yob_`var' > 1967 & yob_`var' < 1985) | (country_to_merge ==111 & inlist(yob_`var', 1974, 1975, 1976))

} 
gen n_UN_gen_G_80and_more = .
replace n_UN_gen_G_80and_more = UN_gen_G_80and_more if (yob_UN_gen_G_80and_more >= GDP_12000_start_year & yob_UN_gen_G_80and_more!=.) | ///
(country_to_merge == 7 & yob_UN_gen_G_80and_more > 1976 & yob_UN_gen_G_80and_more < 1980)  | (country_to_merge == 59 & yob_UN_gen_G_80and_more > 1974 & yob_UN_gen_G_80and_more < 1981) |  ///
(country_to_merge == 92 & yob_UN_gen_G_80and_more > 1967 & yob_UN_gen_G_80and_more < 1985) | (country_to_merge ==111 & inlist(yob_UN_gen_G_80and_more, 1974, 1975, 1976))

*Total of Affluent generations 
egen new_total = rowtotal(n_UN_gen_a_* n_UN_gen_G_80and_more) 

*Share of Affluent generations 
gen share_affluent = new_total/total_VAP
order GDP_12000_start_year, after(share_affluent)

*Exploring the new variable 
fre year if year < 1960 & share_affluent ==. // share_affluent is missing for the pre-1950 data as the UN data starts only in 1950. 
replace share_affluent = 0 if year < 1950 /* But no country could have any affluent cohort before 1950 (the earliest start of affluence is 1941), therefore we can code all pre-1950 shares as zero */
replace share_affluent = 0 if GDP_12000_start_year ==. & year < 2016 // For countries that have not reached the reference gdp level yet, we can code all years as 0 too. 

list country year share_affluent if GDP_12000_start_year ==. // looks good 
list country year share_affluent if share_affluent ==. // looks good (typically the most recent years)
collapse share_affluent gdp_pc GDP_12000_start_year growth_1y growth_2y growth_3y growth_4y, by(country_to_merge year)

lab var gdp_pc "GDP p/c" 
lab var GDP_12000_start_year "Affluence start: $12000"
lab var share_affluent "Affluent Generations"
lab var growth_1y "GDP Growth (1 year)"
lab var growth_2y "GDP Growth (2 years)"
lab var growth_3y "GDP Growth (3 years)"
lab var growth_4y "GDP Growth (4 years)"

rename country_to_merge country


save `"${beg_path}Data\Macro\share_affluent_and_GDP.dta"', replace
